package com.py.custom.ability.threadpool;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * description：线程创建工厂类
 */
public class NamedThreadFactory implements ThreadFactory {

    private final String groupName;

    private AtomicInteger id = new AtomicInteger(1);

    public NamedThreadFactory(String groupName) {
        this.groupName = groupName;
    }

    @Override
    public Thread newThread(Runnable r) {
        String threadName = groupName + "-worker-" + id.incrementAndGet();
        return new Thread(null, r, threadName);
    }
}
